package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.RoleService;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户
 *
 * @author Chengfei
 * @date 2020/08/18
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    @Reference
    private RoleService roleService;

    /**
     * 获取登录用户的用户名
     *
     * @return 用户名
     */
    @RequestMapping("/getUsername")
    public Result getUsername() {

        try {
            org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User)
                    SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }

    }

    /**
     * 分页查询  可带条件
     *
     * @param queryPageBean 封装 当前页码 每页记录数  查询条件
     * @return PageResult  封装  总条数  当前页面结果
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        return userService.findPage(queryPageBean);
    }

    /**
     * 新增用户
     *
     * @param user    用户信息
     * @param roleIds 与用户相关角色id
     * @return true or false
     */
    @RequestMapping("/add")
    public Result add(@RequestBody User user, Integer[] roleIds) {

        try {
            userService.add(user, roleIds);
            return new Result(true, MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }

    }

    /**
     * 查询所有角色组信息
     *
     * @return true or false
     */
    @RequestMapping("/findAll")
    public Result findAll() {

        try {
            List<Role> roleList = roleService.findAllRoles();
            return new Result(true, MessageConstant.GET_ROLES_LIST_SUCCESS, roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ROLES_LIST_FAIL);
        }

    }

    /**
     * 根据id查询用户
     *
     * @param id 用户id
     * @return 用户信息
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {

        try {
            User user = userService.findById(id);
            return new Result(true, MessageConstant.GET_USER_SUCCESS, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USER_FAIL);
        }

    }

    /**
     * 通过用户id查询相应角色id
     *
     * @param id 用户id
     * @return List<Integer> 角色id集合
     */
    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id) {

        try {
            List<Integer> list = userService.findRoleIdsByUserId(id);
            return new Result(true, MessageConstant.GET_ROLEID_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ROLEID_FAIL);
        }

    }

    /**
     * 编辑用户
     *
     * @param user    用户信息
     * @param roleIds 角色id集合
     * @return true or false
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user, Integer[] roleIds) {

        try {
            userService.edit(user, roleIds);
            return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }

    }

    /**
     * 根据用户id删除
     *
     * @param id 用户id
     * @return true or false
     */
    @RequestMapping("/delete")
    public Result delete(Integer id) {

        try {
            userService.delete(id);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }

}
